<?php

namespace Beerus\Modules\BasicSetting\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Beerus\Modules\BasicSetting\Actions\DecoCompanyOperatorTransfer;
use Beerus\Modules\BasicSetting\Actions\DesignerOperatorTransfer;
use Beerus\Modules\BasicSetting\Actions\OperatorTransferBatch;
use Doraemon\model\Operator;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;

class OperatorController extends Controller
{
    const PAGE_SIZE = 20;

    // 对话列表
    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $condition = [];
        if (!empty($request['id'])) {
            $condition['id'] = $request['id'];
        }

        $total = Operator::count($condition);
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        $lists = Operator::select('*', $condition);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();

                $grid->column('id', '运营ID');
                $grid->column('operator_admin_name', '运营后台登录名');
                $grid->column('operator_show_name', '运营展示名');
                $grid->column('operator_type', '运营所属业务线')->display(function() {
                    return Operator::OPERATOR_TYPE_TXT[$this->operator_type];
                });
                $grid->column('data_status', '运营状态')->display(function() {
                    return Operator::DATA_STATUS_TXT[$this->data_status];
                });
                $grid->actions(function(Grid\Displayers\Actions $actions) {
                    $actions->disableView();
                    $actions->disableDelete();
                    if ($this->row['operator_type'] == Operator::OPERATOR_TYPE_DESIGNER_BIND) {
                        $actions->add(new DesignerOperatorTransfer);
                    } else if ($this->row['operator_type'] == Operator::OPERATOR_TYPE_DECO_COMPANY_BIND) {
                        $actions->add(new DecoCompanyOperatorTransfer);
                    }
                });
            }
        );

        $tab = new Tab();
        $tab->add('运营管理', $gridList->render());
        $content->row($tab);
        return $content
            ->header('运营管理')
            ->description(' ')
            ->breadcrumb(['text' => '运营管理']);
    }

    public function edit(Request $request, Content $content, $id = 0) {
        $request = $request->all();

        if (!$id) {
            $is_create = true;
            $header = '新增';
        } else {
            $is_create = false;
            $header = '修改';
        }

        $tab = new Tab();
        $tab->add('运营信息', self::editAction($request, $id, $is_create));
        return $content->header($header)
            ->breadcrumb([
                'text' => '运营信息',
                'url' => route('operator.list')
            ], ['text' => $header])->row($tab);
    }

    public function editAction($request, $id = 0, $isCreate = false)
    {
        if ($isCreate) {
            $data = [];
            $form = new Form($data);
        } else {
            $data = Operator::get('*', ['id' => $id]);
            $form = new Form($data);
            $form->text('id', '运营ID')->setWidth(4, 2)->readOnly();
        }

        $form->text('operator_admin_name', '运营后台登录名')
            ->setWidth(4, 2)
            ->required();
        $form->text('operator_show_name', '运营展示名称')
            ->setWidth(4, 2)
            ->required();
        $form->select('operator_type', '运营所属业务类型')
            ->setWidth(4, 2)
            ->options(Operator::OPERATOR_TYPE_TXT)
            ->default(Operator::OPERATOR_TYPE_DESIGNER_BIND);
        $form->select('data_status', '运营状态')
            ->setWidth(4, 2)
            ->options(Operator::DATA_STATUS_TXT)
            ->default(Operator::STATUS_NORMAL);
        $form->action('/admin/BasicSetting/operator/save');
        return $form->render();
    }

    // 编辑奖项
    public function editSubmit(Request $request)
    {
        $request = $request->all();
        $data = [
            'operator_show_name' => $request['operator_show_name'],
            'operator_admin_name' => $request['operator_admin_name'],
            'operator_type' => $request['operator_type'],
            'data_status' => $request['data_status']
        ];
        if (isset($request['id'])) {
            $rs = Operator::updateInfo($data, ['id' => $request['id']]);
        } else {
            if (Operator::get('*', [
                'operator_type' => $request['operator_type'],
                'OR' => [
                    'operator_admin_name' => $request['operator_admin_name'],
                    'operator_show_name' => $request['operator_show_name']
                ]
            ])) {
                $error = new MessageBag(['title' => '已经存在该运营']);
                return back()->with(compact('error'));
            }
            $rs = Operator::addInfo($data);
        }
        $title = isset($request['id']) ? '修改成功' : '添加成功';
        if (!$rs) {
            $error = new MessageBag(['title' => $title]);
            return back()->with(compact('error'));
        }
        $success = new MessageBag(['title' => $title]);
        return redirect('/admin/BasicSetting/operator')->with(compact('success'));
    }


}
